import { any } from "../../core/logic";
import { Component, ComponentType } from "../Component";
import { color } from "../../core/color"
import { Buffer } from "../../gpu/types/Buffer";
/**
 * 半球光
 * @class
 * @extends w.component.Component
 * @memberof w.light
 */
class HemisLight extends Component{
    /**
     * 
     * @param {String} opts.groundColor 【地面颜色,缺省:"#000000"】
     * @param {String} opts.skyColor 【天空颜色,缺省:"#000000"】
     * @param {*} share 
     */
    constructor(opts={}, share){
        opts.type=any(opts.type,ComponentType.HemisLight);
        super(opts, share);
        this.groundColor =any(opts.groundColor,"#000000");
        this.skyColor=any(opts.skyColor,"#000000"); 
        this.autoUpdate=true;
        this.update();
    }
    /**
     * @property {String} 天空颜色。
     */
    set skyColor(v){
        this._skyColor=v;
        this.addParam("hemisLight");
        this.update();
        return this;
    }
    get skyColor(){
        return this._skyColor;
    }
    /**
     * @property {String} 地面颜色。
     */
    set groundColor(v){
        this._groundColor=v;
        this.addParam("hemisLight");
        this.update();
        return this;
    }
    get groundColor(){
        return this._groundColor;
    }
    toGPU() {
        if(this.has('hemisLight')){
            let lightGPUParameter = this.getParam('hemisLight');
            if (lightGPUParameter) {
                if(!lightGPUParameter.state) {
                    lightGPUParameter.buffer(color.format(this.skyColor, false), 0);
                    lightGPUParameter.buffer(color.format(this.groundColor, false), 4);
                }
            } else {
                let data = new Float32Array(8);
                data.set(color.format(this.skyColor, false), 0);
                data.set(color.format(this.groundColor, false), 4);
                lightGPUParameter = new Buffer({
                    id: "hemisLight",
                    name: "hemisLight",
                    usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
                    visibility: GPUShaderStage.FRAGMENT,
                    type: "uniform",
                    size: data.byteLength,
                    data
                })
                this.addParam('hemisLight',lightGPUParameter);
            }
            lightGPUParameter.state = true;
        }
    }
    updateState(){
        this.setState(true);
    }
}
export {HemisLight}